package ws

import (
	"errors"
)

type ProcMessage struct {
	transmit     IMessageTransmitter
	recv         IMessageReceive
	sendCallback func(interface{})
}

func (p *ProcMessage) SetTransmitter(v IMessageTransmitter) {
	p.transmit = v
}

func (p *ProcMessage) SetReceiver(v IMessageReceive) {
	p.recv = v
}

func (p *ProcMessage) SetSendCallback(fn func(interface{})) {
	p.sendCallback = fn
}

var notHandled = errors.New("Processor: Transimitter nil")

func (p *ProcMessage) ReadMessage(ses ISession) (msg interface{}, err error) {
	if p.transmit != nil {
		return p.transmit.OnRecvMessage(ses)
	}

	return nil, notHandled
}

func (p *ProcMessage) SendMessage(ses ISession, msg interface{}) {
	if p.transmit != nil && ses != nil && msg != nil {
		p.transmit.OnSendMessage(ses, msg)
		if p.sendCallback != nil {
			p.sendCallback(msg)
		}
	}
}

func (p *ProcMessage) ProcData(ses ISession, msg interface{}) {
	if p.recv != nil && msg != nil {
		p.recv.Receive(&MsgEvent{Sess: ses, Msg: msg})
	}
}
